<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/7
 * Time: 10:34
 */

namespace App\Http\Controllers;


use App\Comment;
use App\User;
use App\Film;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    function register(){
        return view('register');
    }
    function check(){
        if(isset($_POST["email"])) {
            $email = $_POST["email"];
            if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
                return "邮箱格式不正确";
            } else {
                $res_email = User::where('email', '=', $email)->get();
                if (count($res_email) == 0) {
                    return "right";
                } else {
                    return "此邮箱已注册";
                }
            }
        }
        if(isset($_POST["nickname"])) {
            $nickname=$_POST["nickname"];
            $res_nickname=User::where('nickname','=',$nickname)->get();
            if(count($res_nickname)==0){
                return "right";
            }
        else{
            return "此昵称已被占用";
            }
        }
    }
    function add(){
        $data=User::create([
            'email'=>$_POST['email'],'password'=>sha1($_POST['password']),'nickname'=>$_POST['nickname'],'sex'=>$_POST['sex']
        ]);
        if($data){
            Session::put('nickname',$_POST['nickname']);
            return '注册成功';
        }
        else{
            echo "注册失败";
        }

    }
    function login(){
        return view('login');
    }
    function verify(){
        $email=$_POST['email'];
        $password=sha1($_POST['password']);
        $res=User::where('email','=',$email)->get();
        if (count($res)>0){
            $res=$res->toArray();
            if($res[0]['password']==$password){
               Session::put('nickname',$res[0]['nickname']);
               return "right";
            }
            else{
                return "密码不正确";
            }
        }
        else{
            return "帐号不存在";
        }
    }
    function logout(){
        Session::forget('nickname');
        return redirect()->back();
    }
    function profile($name){
        $user=User::where('nickname','=',$name)->get();
        $review=Comment::where('user_name','=',$name)->paginate(4);//发表的评论
        $info=array();
        $n=0;
        foreach ($review as $item){
            $info[$n]=Film::find($item['film_id']);
            $item['title']=$info[$n]['title'];
            $item['poster']=$info[$n]['poster'];
            $n++;
        }
        $id=$user[0]->id;
        $comment=Comment::where('assist_user','like',$id)->paginate(4);//赞过的评论
        foreach ($comment as $item){
            $info[$n]=Film::find($item['film_id']);
            $item['title']=$info[$n]['title'];
            $item['poster']=$info[$n]['poster'];
            $n++;
        }
        return view('profile',['user'=>$user,'name'=>$name,'review'=>$review,'comment'=>$comment]);
    }
    function save(){
        $name=$_POST['name'];
        $sex=$_POST['sex'];
        $pro=$_POST['pro'];
        $city=$_POST['city'];
        $sign=$_POST['sign'];
        $user=User::where('nickname','=',$name)->get();
        foreach ($user as $user){
        $user->sex=$sex;
        $user->profession=$pro;
        $user->city=$city;
        $user->sign=$sign;
        }
        $user=$user->save();
        if($user){
            return 'success';
        }
        else{
            return 'error';
        }
    }
    function test(){
        $str='583482081';
        $en=sha1($str);
        echo "fd5f5f91c6902e4a6925e7706cbb47db446a265b";
        echo "fd5f5f91c6902e4a6925e7706cbb47db446a265b";
    }
}